﻿using Serilog;
using Serilog.Core;
using Serilog.Events;
using Serilog.Debugging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CKS.Core.Framework
{
    /// <summary>
    /// 输出日志到文件夹
    /// </summary>
    public static class LogHelper
    {
        static LogHelper()
        {
            Init();
        } 

       private static ILogger RequestLogger { get; set; }
        private static ILogger ErrorLogger { get; set; }

        public static ILogger MainLogger { get; set; }

        public static ILogger PlcLogger { get; set; }
        public static void Init()
        {
            string SerilogOutputTemplate = "{Timestamp:yyyy-MM-dd HH:mm:ss.fff}[{Level}] {Message:lj}{NewLine}{Exception}";
            MainLogger = new LoggerConfiguration()
            .WriteTo.Console()
            .WriteTo.Debug()
            //.WriteTo.File(Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, @"logs/log_.txt"),
            //    rollingInterval: RollingInterval.Day,
            //    outputTemplate: SerilogOutputTemplate,
            //    rollOnFileSizeLimit: true)
             .WriteTo.File(Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, @"logs/log_.txt"),
               // levelSwitch: new LoggingLevelSwitch(Serilog.Events.LogEventLevel.Information),
                rollingInterval: RollingInterval.Day,
                outputTemplate: SerilogOutputTemplate,
                rollOnFileSizeLimit: true)
            .CreateLogger();



            ErrorLogger =
             new LoggerConfiguration()
         .WriteTo.Console()
            .WriteTo.Debug()
         .WriteTo.File(Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, @"logs/err_.txt"),
             rollingInterval: RollingInterval.Day,
             outputTemplate: SerilogOutputTemplate,
             rollOnFileSizeLimit: true).CreateLogger();

            string SerilogOutputTemplate2 = "{Timestamp:yyyy-MM-dd HH:mm:ss.fff} {Message:lj}{NewLine}{Exception}";

            RequestLogger =
                new LoggerConfiguration()
            //.WriteTo.Console()
            .WriteTo.File(Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, @"logs/Request_.txt"),
                rollingInterval: RollingInterval.Day,
                outputTemplate: SerilogOutputTemplate2,
                rollOnFileSizeLimit: true)
            //.WriteTo.File("logs/err_.txt",
            //    levelSwitch: new LoggingLevelSwitch(Serilog.Events.LogEventLevel.Error),
            //    rollingInterval: RollingInterval.Day,
            //    outputTemplate: SerilogOutputTemplate,
            //    rollOnFileSizeLimit: true)
            .CreateLogger();




            PlcLogger = new LoggerConfiguration()
            .Enrich.FromLogContext()
            .WriteTo.Console()
            .WriteTo.Debug()
            .WriteTo.File(Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, @"logs/plc_.txt"),
            rollingInterval: RollingInterval.Day,
                outputTemplate: SerilogOutputTemplate2,
                rollOnFileSizeLimit: true
            ).CreateLogger();
           //.CreateBootstrapLogger();
        }


        public static void NewRequest(string message)
        {
            RequestLogger.Information(message);
        }

        public static void WriteLog(this string message)
        {
            MainLogger.Information(message);
            
        }

        public static void WriteErrorLog(this string message)
        {
            ErrorLogger.Error(message);
        }

        public static void WriteLog(this string message, Exception exception)
        {
            MainLogger.Information(exception, message);
        }
        /// <summary>
        /// 写入和PLC相关的日志
        /// </summary>
        /// <param name="message"></param>
        /// <param name="level"></param>
        public static void WritePlcLog(this string message, LogEventLevel level = LogEventLevel.Information)
        {
            PlcLogger.Write(level, message);
        }
        
    }
}
